package com.example.xinhuazidian

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.example.xinhuazidian.utils.JsonFileUtil
import com.example.xinhuazidian.adapter.Qianjiashi2Adapter
import com.example.xinhuazidian.adapter.QianjiashiAdapter
import com.example.xinhuazidian.bean.Qianjiashi
import com.example.xinhuazidian.databinding.ActivityQianjiashiBinding
import com.google.gson.Gson
import com.gyf.immersionbar.ImmersionBar

/**
 * 千家诗
 */
class QianjiashiActivity : AppCompatActivity(), QianjiashiAdapter.OnRecyclerItemClickListener {

    private lateinit var binding: ActivityQianjiashiBinding
    private var qianjiashiAdapter: QianjiashiAdapter? = null
    private var str: String? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityQianjiashiBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initView()
        initData()
    }

    private fun initData() {
        qianjiashiAdapter?.setRecyclerItemClickListener(this)
    }

    private fun initView() {
        //沉浸状态栏
        ImmersionBar.with(this)
            .statusBarDarkFont(true)//状态栏字体是深色，不写默认为亮色
            .init()

        load()
    }

    @SuppressLint("SetTextI18n")
    private fun load() {
        str = JsonFileUtil.loadFile(this, "qianjiashi.json")

        val bean = Gson().fromJson(str, Qianjiashi::class.java)
        initAdapter(bean.content)
        beanX = bean.content[0].content
        initAdapter2(beanX)

        binding.tvTitle.text = bean.title
        binding.tvNum.text = "${bean.content[0].content.size}首"
    }

    private fun initAdapter(list: MutableList<Qianjiashi.ContentBeanX>) {
        val staggeredGridLayoutManager =
            StaggeredGridLayoutManager(1, LinearLayoutManager.HORIZONTAL)
        binding.rv.layoutManager = staggeredGridLayoutManager

        //加载适配器
        qianjiashiAdapter = QianjiashiAdapter(this, list)
        binding.rv.adapter = qianjiashiAdapter
    }

    private fun initAdapter2(list: MutableList<Qianjiashi.ContentBeanX.ContentBean>) {
        val staggeredGridLayoutManager = StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL)
        binding.rv2.layoutManager = staggeredGridLayoutManager

        //加载适配器
        val qianjiashi2Adapter = Qianjiashi2Adapter(list)
        binding.rv2.adapter = qianjiashi2Adapter

        qianjiashi2Adapter.setRecyclerItem2ClickListener(object :
            Qianjiashi2Adapter.OnRecyclerItem2ClickListener {
            override fun onRecyclerItem2Click(position: Int) {
                intent = Intent(this@QianjiashiActivity, QianjiashiXiangqingActivity::class.java)
                intent.putExtra("bean", Gson().toJson(beanX[position]))
                startActivity(intent)
            }
        })
    }

    private lateinit var beanX: MutableList<Qianjiashi.ContentBeanX.ContentBean>

    @SuppressLint("SetTextI18n")
    override fun onRecyclerItemClick(position: Int) {
        val bean = Gson().fromJson(str, Qianjiashi::class.java)
        beanX = bean.content[position].content
        initAdapter2(beanX)

        binding.tvNum.text = "${bean.content[position].content.size}首"
    }
}